﻿using RWSBeeldbank.Common;
using RWSBeeldbank.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.DataTransfer;
using Windows.ApplicationModel.Resources;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace RWSBeeldbank
{
    public sealed partial class PreferencesUserControl : UserControl
    {
        public PreferencesUserControl()
        {
            this.InitializeComponent();
                        
            if (ApplicationData.Current.RoamingSettings.Values.ContainsKey("MediaType"))
            {
                Digital.IsChecked = Convert.ToInt16(ApplicationData.Current.RoamingSettings.Values["MediaType"]) == Constants.ITEM_TYPE_DIGITAL;
                Analog.IsChecked = Convert.ToInt16(ApplicationData.Current.RoamingSettings.Values["MediaType"]) == Constants.ITEM_TYPE_ANALOG;
                Video.IsChecked = Convert.ToInt16(ApplicationData.Current.RoamingSettings.Values["MediaType"]) == Constants.ITEM_TYPE_VIDEO;
            }
        }

        private async void HandleCheck(object sender, RoutedEventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            int mediaTypeValue = Constants.ITEM_TYPE_DIGITAL;
            switch (rb.Name)
            {
                case "Digital":
                    {
                        break;
                    }
                case "Analog":
                    {
                        mediaTypeValue = Constants.ITEM_TYPE_ANALOG;
                        break;
                    }
                case "Video":
                    {
                        mediaTypeValue = Constants.ITEM_TYPE_VIDEO;
                        break;
                    }
            }
            if (Convert.ToInt16(ApplicationData.Current.RoamingSettings.Values["MediaType"]) != mediaTypeValue)
            {
                // Set roaming value to new value;
                ApplicationData.Current.RoamingSettings.Values["MediaType"] = mediaTypeValue;

                // Reload data in the app
                try
                {
                    // Clear memory and reload app with new value for TypeId
                    MediaObjectDataSource.Clear();

                    await MediaObjectDataSource.LoadRemoteDataAsync();

                    // Add a Frame control to the window
                    var rootFrame = new Frame();
                    SuspensionManager.RegisterFrame(rootFrame, "AppFrame");

                    if (!rootFrame.Navigate(typeof(GroupedItemsPage), "AllGroups"))
                    {
                        throw new Exception("Failed to create initial page");
                    }

                    // Place the frame in the current Window and ensure that it is active
                    Window.Current.Content = rootFrame;
                }
                catch
                {
                    showMessagebox();
                }
            }
        }

        private async void showMessagebox()
        {
            // Load resource handler
            var resourceLoader = new ResourceLoader();

            // Create the message dialog and set its content
            var messageDialog = new MessageDialog(resourceLoader.GetString("ErrorNoInternet"));

            // Add commands and set their callbacks; both buttons use the same callback function instead of inline event handlers
            messageDialog.Commands.Add(new UICommand(resourceLoader.GetString("CloseButton"), new UICommandInvokedHandler(this.CommandInvokedHandler)));

            // Set the command that will be invoked by default
            messageDialog.DefaultCommandIndex = 0;

            // Set the command to be invoked when escape is pressed
            messageDialog.CancelCommandIndex = 1;

            // Show the message dialog
            await messageDialog.ShowAsync();
        }

        private void CommandInvokedHandler(IUICommand command)
        {
            App.Current.Exit();
        }
    }
}
